package shiro;


import com.alibaba.fastjson.JSON;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lzh
 * @description 对返回结果的统一封装
 * @date 2021/2/7
 */
public class R implements Serializable {
    private int status;
    private String msg;
    private Object data;

    /**
     * 建造者模式，用于构建data是json的返回结果
     */
    static class Builder {
        private Map<String, Object> data;

        public Builder() {
            data = new HashMap<>();
        }

        public Builder put(String name, Object val) {
            data.put(name, val);
            return this;
        }

        public R succ() {
            return R.msg(200, "", data);
        }
    }

    public static Builder data() {
        return new Builder();
    }

    public static R msg(int code, String msg, Object data) {
        R res = new R(code, msg, data);
        return res;
    }

    public static R succ() {
        return msg(200, "操作成功", null);
    }

    public static R succ(Object data) {
        return msg(200, "", data);
    }

    public static R fail(int code, String msg) {
        return msg(code, msg, null);
    }

    public R(int code, String msg, Object data) {
        this.status = code;
        this.msg = msg;
        this.data = data;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int code) {
        this.status = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + status +
                ", msg='" + msg + "'" +
                ", data=" + JSON.toJSONString(data) +
                '}';
    }
}